<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .right {
        color: green;
      }
      .wrong {
        color: red;
      }
    </style>
  </head>
  <body>
    <input type="text" class="uname" /><span>请输入用户名</span>
    <script>
      var reg = /^[a-zA-Z0-9_-]{6,16}$/; // 限制6到16位 数字 字母 _ -
      var unmae = document.querySelector(".uname");
      var span = document.querySelector("span");
      unmae.onblur = function () {
        if (reg.test(this.value)) {
          span.className = "right";
          span.innerHTML = "输入正确";
        } else {
          span.className = "wrong";
          span.innerHTML = "输入有误";
        }
      };
    </script>
  </body>
</html>
